OpenRoads Designer CONNECT Edition SDK Help

QQChecker

Description

  • This is custom class for generating data of alignment/profile and terrain elevation differences .

  • Users need to set active terrain and active profile for selected alignment.

  • TerrainReport3dLineString() and TerrainReportProfile() functions create data for calculating elevation and differences.

Remarks

  • This sample code is a part of ManagedSDKExample which you get with SDK installation under "examples" section in SDK installation directory.

  • This code is simplified and does not include report generation code, but user can get it under ManagedSDKExample ->Examples ->QQChecker

Source Code


using System;
using System.Collections.Generic;
using System.Text;
using Bentley.DgnPlatformNET;
using Bentley.DgnPlatformNET.Elements;
using Bentley.CifNET.GeometryModel.SDK;
using Bentley.CifNET.SDK;
using Bentley.CifNET.LinearGeometry;
using Bentley.CifNET.Formatting;

namespace ManagedSDKExample.Examples
{

    class QQChecker : DgnElementSetTool
    {
        ConsensusConnection m_con;
        internal static DgnModel m_activeModel = Bentley.MstnPlatformNET.Session.Instance.GetActiveDgnModel();
        internal static double m_defaultUnitsToMeters = FormatSettingsConstants.GetMasterUnitsToMeters();

        public QQChecker() : base()
        {
        }

        protected override void OnRestartTool()
        {
            InstallNewInstance();
        }

        protected override void ExitTool()
        {
            if (m_con != null)
            {
                m_con.Close();
                m_con.Dispose();
            }
            m_con = null;
            base.ExitTool();
        }


        protected override void OnPostInstall()
        {
            base.BeginPickElements();
            Bentley.DgnPlatformNET.AccuSnap.LocateEnabled = true;
            Bentley.DgnPlatformNET.AccuSnap.SnapEnabled = true;
            m_con = Bentley.CifNET.SDK.Edit.ConsensusConnectionEdit.GetActive();
            if (m_con == null)
                return;
            NotificationManager.OutputPrompt("Select an alignment to report.");
            base.OnPostInstall();
        }


        protected override bool OnDataButton(Bentley.DgnPlatformNET.DgnButtonEvent ev)
        {
            Bentley.DgnPlatformNET.HitPath hitPath = DoLocate(ev, true, 1);
            if (hitPath == null)
                return false;

            Element el = hitPath.GetCursorElement();

            if (el == null)
                return false;

            Alignment al = (el.ParentElement == null) ? Alignment.CreateFromElement(m_con, el) : Alignment.CreateFromElement(m_con, el.ParentElement);

            if (al == null)
            {
                if (el.ElementType == MSElementType.LineString)
                    TerrainReport3dLineString(el);
                else
                    return false;
            }
            else
                TerrainReportProfile(al);

            return true;
        }

        public override Bentley.DgnPlatformNET.StatusInt OnElementModify(Bentley.DgnPlatformNET.Elements.Element element)
        {
            return Bentley.DgnPlatformNET.StatusInt.Error;
        }

        protected override bool OnResetButton(Bentley.DgnPlatformNET.DgnButtonEvent ev)
        {
            ExitTool();
            return true;
        }


        public static void InstallNewInstance()
        {
            QQChecker tool = new QQChecker();
            tool.InstallTool();
        }

        /*------------------------------------------------------------------------------------**/
        /* Creates report of difference between terrain and alignment elevations.
        /*--------------+---------------+---------------+---------------+---------------+------*/
        public void TerrainReportProfile(Alignment al)
        {
            ConsensusConnection sdkCon = Bentley.CifNET.SDK.Edit.ConsensusConnectionEdit.GetActive();
            if (sdkCon == null)
                return;

            GeometricModel geomModel = sdkCon.GetActiveGeometricModel();
            if (geomModel == null)
                return;

            SurfaceEntity activeSurface = geomModel.ActiveSurface;

            Dictionary<string, string> header = new Dictionary<string, string>();
            header.Add("File Name", geomModel.DgnModel.GetDgnFile().GetFileName());
            header.Add("Model Name", geomModel.DgnModel.ModelName);
            string surfaceName = "No Active Terrain";
            if (activeSurface != null)
            {
                surfaceName = string.IsNullOrEmpty(activeSurface.Name) ? "Unnamed" : activeSurface.Name;
            }
            header.Add("Active Terrain Name", surfaceName);


            StringBuilder sb = new StringBuilder();
            StationFormatSettings settings = StationFormatSettings.GetStationFormatSettingsForModel(Bentley.MstnPlatformNET.Session.Instance.GetActiveDgnModelRef() as DgnModel);
            StationingFormatter formatter = new StationingFormatter(al);
            Profile activeProfile = al.ActiveProfile;

            sb.Clear();

            sb.Append("Horizontal Alignment: ");
            sb.Append(string.IsNullOrEmpty(al.Name) ? "Unnamed" : al.Name);

            sb.Append("Active Vertical Alignment Name: ");
            if (activeProfile != null)
            {
                sb.Append(string.IsNullOrEmpty(activeProfile.Name) ? "Unnamed" : activeProfile.Name);
            }
            else
            {
                sb.Append("No Active Vertical Alignment");
            }

            int pointCount = 0;

            if (activeSurface != null && activeSurface is TerrainSurface)
            {
                TerrainSurface surface = activeSurface as TerrainSurface;

                List<LinearElement> segments = new List<LinearElement>();
                if (al.LinearGeometry is LinearComplex)
                {
                    LinearComplex lc = (al.LinearGeometry as LinearComplex);
                    foreach (LinearElement le in lc.GetSubLinearElements())
                    {
                        segments.Add(le);
                    }
                }
                else if (al.LinearGeometry is LineString)
                {
                    LineString ls = (al.LinearGeometry as LineString);
                    Bentley.GeometryNET.DPoint3d[] vertices = ls.GetVertices();
                    for (int i = 0; i < vertices.Length - 1; i++)
                    {
                        segments.Add(new Line3d(vertices[i], vertices[i + 1]));
                    }
                }
                else
                {
                    segments.Add(al.LinearGeometry);
                }

                //do first point before loop, only have to worry about second point in each linear element
                Bentley.TerrainModelNET.DTMDrapedPoint start = surface.DTM?.DrapePoint(segments[0].StartPoint.Coordinates);
                string xCoordinate = FormatNumber(ConvertMeterToMaster(segments[0].StartPoint.Coordinates.X));
                string yCoordinate = FormatNumber(ConvertMeterToMaster(segments[0].StartPoint.Coordinates.Y));
                string length = "";
                string terrainElevation = "";
                if (start != null)
                {
                    switch (start.Code)
                    {
                        case Bentley.TerrainModelNET.DTMDrapedPointCode.PointOrSide:
                        case Bentley.TerrainModelNET.DTMDrapedPointCode.Triangle:
                            terrainElevation = FormatNumber(ConvertMeterToMaster(start.Coordinates.Z));
                            break;
                        case Bentley.TerrainModelNET.DTMDrapedPointCode.External:
                            terrainElevation = "<font color=\"ff0000\"><em>External</em></font>";
                            break;
                        case Bentley.TerrainModelNET.DTMDrapedPointCode.Void:
                            terrainElevation = "<font color=\"ff0000\"><em>Void</em></font>";
                            break;
                        default:
                            break;
                    }
                }

                double elevation = Double.NaN;
                double distanceAlong = Double.NaN;
                string checkElevation = "";
                string elevationDifference = "";
                if (al.ActiveProfile != null)
                {
                    distanceAlong = segments[0].StartPoint.DistanceAlong;
                    // Active Profile may be shorter than the Horizontal Alignment
                    if (distanceAlong < al.ActiveProfile.ProfileGeometry.Length)
                    {
                        LinearPoint pointAtDistanceAlong = al.ActiveProfile.ProfileGeometry.GetPointAtX(distanceAlong);
                        if (pointAtDistanceAlong != null)
                        {
                            elevation = pointAtDistanceAlong.Coordinates.Y;
                            checkElevation = FormatNumber(ConvertMeterToMaster(elevation));
                            if (start != null && (start.Code == Bentley.TerrainModelNET.DTMDrapedPointCode.PointOrSide || start.Code == Bentley.TerrainModelNET.DTMDrapedPointCode.Triangle))
                                elevationDifference = FormatNumber(ConvertMeterToMaster(start.Coordinates.Z - elevation));

                            pointCount++;
                        }
                    }
                }

                foreach (LinearElement le in segments)
                {
                    Bentley.TerrainModelNET.DTMDrapedPoint end = surface.DTM?.DrapePoint(le.EndPoint.Coordinates);
                    xCoordinate = FormatNumber(ConvertMeterToMaster(le.EndPoint.Coordinates.X));
                    yCoordinate = FormatNumber(ConvertMeterToMaster(le.EndPoint.Coordinates.Y));
                    length = FormatNumber(ConvertMeterToMaster(le.Length));
                    terrainElevation = "";
                    if (end != null)
                    {
                        switch (end.Code)
                        {
                            case Bentley.TerrainModelNET.DTMDrapedPointCode.PointOrSide:
                            case Bentley.TerrainModelNET.DTMDrapedPointCode.Triangle:
                                terrainElevation = FormatNumber(ConvertMeterToMaster(end.Coordinates.Z));
                                break;
                            case Bentley.TerrainModelNET.DTMDrapedPointCode.External:
                                terrainElevation = "<font color=\"ff0000\"><em>External</em></font>";
                                break;
                            case Bentley.TerrainModelNET.DTMDrapedPointCode.Void:
                                terrainElevation = "<font color=\"ff0000\"><em>Void</em></font>";
                                break;
                            default:
                                break;
                        }
                    }

                    elevation = Double.NaN;
                    checkElevation = "";
                    elevationDifference = "";
                    if (al.ActiveProfile != null)
                    {
                        distanceAlong += le.EndPoint.DistanceAlong;
                        // Active Profile may be shorter than the Horizontal Alignment
                        if (distanceAlong < al.ActiveProfile.ProfileGeometry.Length)
                        {
                            LinearPoint pointAtDistanceAlong = al.ActiveProfile.ProfileGeometry.GetPointAtX(distanceAlong);
                            if (pointAtDistanceAlong != null)
                            {
                                elevation = pointAtDistanceAlong.Coordinates.Y;
                                checkElevation = FormatNumber(ConvertMeterToMaster(elevation));
                                if (end != null && (end.Code == Bentley.TerrainModelNET.DTMDrapedPointCode.PointOrSide || end.Code == Bentley.TerrainModelNET.DTMDrapedPointCode.Triangle))
                                    elevationDifference = FormatNumber(ConvertMeterToMaster(end.Coordinates.Z - elevation));

                                pointCount++;

                            }
                        }
                    }
                }
            }
        }

        public void TerrainReport3dLineString(Element el)
        {

            ConsensusConnection sdkCon = Bentley.CifNET.SDK.Edit.ConsensusConnectionEdit.GetActive();
            if (sdkCon == null)
                return;

            GeometricModel geomModel = sdkCon.GetActiveGeometricModel();
            if (geomModel == null)
                return;

            SurfaceEntity activeSurface = geomModel.ActiveSurface;

            Dictionary<string, string> header = new Dictionary<string, string>();
            header.Add("File Name", geomModel.DgnModel.GetDgnFile().GetFileName());
            header.Add("Model Name", geomModel.DgnModel.ModelName);
            string surfaceName = "No Active Terrain";
            if (activeSurface != null)
            {
                surfaceName = string.IsNullOrEmpty(activeSurface.Name) ? "Unnamed" : activeSurface.Name;
            }
            header.Add("Active Terrain Name", surfaceName);

            StringBuilder sb = new StringBuilder();
            sb.Clear();

            sb.Append("Horizontal Alignment: ");
            sb.Append("Unnamed");

            sb.Append("Active Vertical Alignment Name: ");
            sb.Append("No Active Vertical Alignment");

            int pointCount = 0;

            if (activeSurface != null && activeSurface is TerrainSurface && el.ElementType == MSElementType.LineString)
            {
                TerrainSurface surface = activeSurface as TerrainSurface;

                List<LinearElement> segments = new List<LinearElement>();

                Bentley.DgnPlatformNET.Elements.LineStringElement lineString = el as Bentley.DgnPlatformNET.Elements.LineStringElement;
                List<Bentley.GeometryNET.DPoint3d> pointList = new List<Bentley.GeometryNET.DPoint3d>();
                Bentley.GeometryNET.CurvePrimitive curvePrim = lineString.GetCurveVector().GetPrimitive(0);
                curvePrim.TryGetLineString(pointList);

                for (int i = 0; i < pointList.Count - 1; i++)
                {
                    Bentley.GeometryNET.DPoint3d point1 = pointList[i];
                    Bentley.GeometryNET.DPoint3d point2 = pointList[i + 1];
                    point1.Set(ConvertUORToMeter(point1.X), ConvertUORToMeter(point1.Y), ConvertUORToMeter(point1.Z));
                    point2.Set(ConvertUORToMeter(point2.X), ConvertUORToMeter(point2.Y), ConvertUORToMeter(point2.Z));
                    segments.Add(new Line3d(point1, point2));
                }

                //do first point before loop, only have to worry about second point in each linear element
                Bentley.TerrainModelNET.DTMDrapedPoint start = surface.DTM?.DrapePoint(segments[0].StartPoint.Coordinates);
                string xCoordinate = FormatNumber(ConvertMeterToMaster(segments[0].StartPoint.Coordinates.X));
                string yCoordinate = FormatNumber(ConvertMeterToMaster(segments[0].StartPoint.Coordinates.Y));
                string length = "";
                string terrainElevation = "";
                if (start != null)
                {
                    switch (start.Code)
                    {
                        case Bentley.TerrainModelNET.DTMDrapedPointCode.PointOrSide:
                        case Bentley.TerrainModelNET.DTMDrapedPointCode.Triangle:
                            terrainElevation = FormatNumber(ConvertMeterToMaster(start.Coordinates.Z));
                            break;
                        case Bentley.TerrainModelNET.DTMDrapedPointCode.External:
                            terrainElevation = "<font color=\"ff0000\"><em>External</em></font>";
                            break;
                        case Bentley.TerrainModelNET.DTMDrapedPointCode.Void:
                            terrainElevation = "<font color=\"ff0000\"><em>Void</em></font>";
                            break;
                        default:
                            break;
                    }
                }

                double elevation = Double.NaN;
                double distanceAlong = Double.NaN;
                string checkElevation = "";
                string elevationDifference = "";
                distanceAlong = segments[0].StartPoint.DistanceAlong;
                elevation = segments[0].StartPoint.Coordinates.Z;
                checkElevation = FormatNumber(ConvertMeterToMaster(elevation));
                if (start != null && (start.Code == Bentley.TerrainModelNET.DTMDrapedPointCode.PointOrSide || start.Code == Bentley.TerrainModelNET.DTMDrapedPointCode.Triangle))
                    elevationDifference = FormatNumber(ConvertMeterToMaster(start.Coordinates.Z - elevation));
                pointCount++;

                foreach (LinearElement le in segments)
                {
                    Bentley.TerrainModelNET.DTMDrapedPoint end = surface.DTM?.DrapePoint(le.EndPoint.Coordinates);
                    xCoordinate = FormatNumber(ConvertMeterToMaster(le.EndPoint.Coordinates.X));
                    yCoordinate = FormatNumber(ConvertMeterToMaster(le.EndPoint.Coordinates.Y));
                    length = FormatNumber(ConvertMeterToMaster(le.Length));
                    terrainElevation = "";
                    if (end != null)
                    {
                        switch (end.Code)
                        {
                            case Bentley.TerrainModelNET.DTMDrapedPointCode.PointOrSide:
                            case Bentley.TerrainModelNET.DTMDrapedPointCode.Triangle:
                                terrainElevation = FormatNumber(ConvertMeterToMaster(end.Coordinates.Z));
                                break;
                            case Bentley.TerrainModelNET.DTMDrapedPointCode.External:
                                terrainElevation = "<font color=\"ff0000\"><em>External</em></font>";
                                break;
                            case Bentley.TerrainModelNET.DTMDrapedPointCode.Void:
                                terrainElevation = "<font color=\"ff0000\"><em>Void</em></font>";
                                break;
                            default:
                                break;
                        }
                    }

                    elevation = Double.NaN;
                    checkElevation = "";
                    elevationDifference = "";
                    distanceAlong += le.EndPoint.DistanceAlong;
                    elevation = le.EndPoint.Coordinates.Z;
                    checkElevation = FormatNumber(ConvertMeterToMaster(elevation));
                    if (end != null && (end.Code == Bentley.TerrainModelNET.DTMDrapedPointCode.PointOrSide || end.Code == Bentley.TerrainModelNET.DTMDrapedPointCode.Triangle))
                        elevationDifference = FormatNumber(ConvertMeterToMaster(end.Coordinates.Z - elevation));
                    pointCount++;

                }
            }
        }
        public static string FormatNumber(double num)
        {
            return FormatForDisplay.Double(num);
        }
        public static double ConvertMeterToMaster(double num)
        {
            return num / m_defaultUnitsToMeters;
        }
        public static string FormatDistance(double value)
        {
            return FormatForDisplay.Distance(value, m_activeModel, 3);
        }
        public static double ConvertUORToMeter(double num)
        {
            ModelInfo info = m_activeModel.GetModelInfo();
            return num / info.UorPerMeter;
        }
    }
}